<?php

use yii\db\Migration;

class m170425_055129_create_init_attendance_module_tables extends Migration
{
    public function safeUp()
    {
         $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        //1.创建部门表
        $this->createTable('ts_department', [
            'id' => $this->primaryKey(),
            'department_id' => $this->Integer()->unsigned()->notNull()->defaultValue(1)->comment('部门id'),
            'name' => $this->string(32)->notNull()->comment('部门名称'),
            'parentid' => $this->Integer()->unsigned()->notNull()->defaultValue(1)->comment('父级id'),
            'order' => $this->smallInteger()->unsigned()->notNull()->defaultValue(1)->comment('排序'),
            'path'=>$this->string(100)->defaultValue('')->comment('级别路径'),
            'corpid' => $this->string(18)->defaultValue('')->comment('企业id'),
            ], $tableOptions);
        $this->createIndex('corpid','ts_department','corpid');
        //2.创建员工表
        $this->createTable('ts_department_user',[
            'id'=>$this->primaryKey(),
            'userid'=>$this->string(64)->notNull()->comment('成员userid'),
            'name'=>$this->string(50)->notNull()->comment('成员名称'),
            'department'=>$this->string(20)->notNull()->comment('成员所属部门'),
            'position'=>$this->string(50)->comment('职位信息'),
            'mobile'=>$this->string(11)->defaultValue('请输手机号')->comment('手机号码'),
            'gender'=>$this->smallInteger()->defaultValue(0)->comment('性别'),
            'email'=>$this->string(64)->comment('电子邮箱'),
            'weixinid'=>$this->string(64)->comment('微信号'),
            'avatar'=>$this->string()->comment('头像url'),
            'status'=>$this->smallInteger()->defaultValue(4)->comment('关注状态'),
            'extattr'=>$this->string()->comment('扩展属性'),
            'leader'=>$this->smallInteger()->defaultValue(10)->comment('是否领导'),
            'state'=>$this->smallInteger()->defaultValue(2)->comment('是否在单位'),
            'update_at'=>$this->datetime()->comment('更新时间'),
            'lng'=>$this->decimal(10,6)->defaultValue('114.307614')->comment('经度'),
            'lat'=>$this->decimal(10,6)->defaultValue('29.879616')->comment('纬度'),
            'corpid' => $this->string(18)->defaultValue('')->comment('企业id'),
            'admin'=>$this->smallInteger()->defaultValue(0)->comment('是否是管理员'),
            ],$tableOptions);
        $this->createIndex('corpid','ts_department_user','corpid');
        $this->createIndex('mobile','ts_department_user','mobile');
        //3.创建考勤表 一个工作日两次上下班 两次旷工，按次数统计，情非得已加了个旷工的字段
        // $this->createTable('ts_attendance', [
        //     'id' => $this->primaryKey(),
        //     'mobile' => $this->string(11)->notNull()->comment('手机号码'),
        //     'create_at' => $this->date()->notNull()->comment('创建日期'),
        //     'attendance' => $this->smallInteger()->notNull()->defaultValue(1)->comment('是否全勤'),
        //     'kuanggong'=>$this->smallInteger()->defaultValue(1)->comment('旷工次数'),
        //     'remarks'=>$this->string()->comment('备注'),
        //     'corpid' => $this->string(18)->defaultValue('')->comment('企业id'),
        //     ], $tableOptions);
        // $this->createIndex('corpid','ts_attendance','corpid');
        // $this->createIndex('mobile','ts_attendance','mobile');
        //4.创建电子围栏表
        $this->createTable('ts_fence', [
            'id' => $this->primaryKey(),
            'attendance_id' => $this->Integer()->notNull()->comment('考勤id'),
            'create_at' => $this->datetime()->notNull()->comment('创建时间'),
            'pass' => $this->smallInteger()->notNull()->defaultValue(1)->comment('进出电子围栏'),
            ], $tableOptions);
        $this->createIndex('attendance_id','ts_fence','attendance_id');
        //5.配置表
        $this->createTable('ts_department_configuration', [
            'id' => $this->primaryKey(),
            'department_id' => $this->Integer()->notNull()->comment('单位id'),
            'name' => $this->string(50)->notNull()->comment('配置名称'),
            'value' => $this->string()->comment('配置值'),
            'corpid' => $this->string(18)->defaultValue('')->comment('企业id'),
            ], $tableOptions);
        $this->createIndex('corpid','ts_department_configuration','corpid');
        //6.工作日表
        $this->createTable('ts_workday', [
            'id' => $this->primaryKey(),
            'date' => $this->date()->notNull()->comment('日期'),
            'is_work_day' => $this->smallInteger()->notNull()->comment('是否工作日'),
            'corpid'=>$this->string(18)->comment('企业id'),
            ], $tableOptions);
        $this->createIndex('date','ts_workday','date');
        //7.管理员表
        $this->createTable('ts_admin', [
            'id' => $this->primaryKey(),
            'admin' => $this->string(20)->notNull()->comment('管理员名称'),
            'password' => $this->string(32)->notNull()->comment('密码'),
            'corpid' => $this->string(20)->notNull()->comment('企业id'),
            'secret'=>$this->string(100)->notNull()->comment('管理组密码'),
            'login_at'=>$this->datetime()->comment('登录时间'),
            'login_ip'=>$this->string()->comment('登录ip'),
            'auth'=>$this->string(20)->comment('权限'),
            ], $tableOptions);
        $this->insert('ts_admin', [
            'admin' => 'admin',
            'password' => 'admin',
            'corpid'=>'ww77f4d849ed4c3092',
            'secret'=>'ipuzeCc_5l8xnMoH4153HrBRTw2gABZ3d47dvxI6Nt4',
            'login_at'=>'2017-05-01 12:23:12',
            'login_ip'=>'233.15.34.234',
            'auth'=>'admin',
        ]);  
    }
    
    public function safeDown()
    {
        echo "m170425_055129_create_init_attendance_module_tables cannot be reverted.\n";
        $this->dropTable('ts_admin');
        $this->dropTable('ts_workday');
        $this->dropTable('ts_department_configuration');
        $this->dropTable('ts_fence');
        // $this->dropTable('ts_attendance');
        $this->dropTable('ts_department_user');
        $this->dropTable('ts_department');
    }
}
